#include <linux/module.h>
#include <linux/init.h>

#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/mm.h>
#include <linux/ioport.h>
#include <linux/interrupt.h>
#include <linux/delay.h>
#include <linux/device.h>

#include <asm/io.h>

#include "bus.h"

MODULE_LICENSE("GPL");
MODULE_AUTHOR("zl");

int zl_match(struct device *  dev, struct device_driver * drv);

struct bus_type zl_bus = {
    .name  = "zlbus", //this string will be show as /sys/bus/zlbus
    .match = zl_match,
};

EXPORT_SYMBOL(zl_bus);


int zl_device_register(struct zl_device *dev)
{
    dev->dev.bus = &zl_bus;    
    //strcpy(dev->dev.bus_id, dev->name); //2.6.13

    dev->dev.init_name = dev->name; //2.6.36

    printk("[%s %s %d ]\n", __FILE__,  __FUNCTION__, __LINE__); 
    device_register(&dev->dev);

    return 0;
}
EXPORT_SYMBOL(zl_device_register);

int zl_device_unregister(struct zl_device *dev)
{
    printk("[%s %s %d ]\n", __FILE__,  __FUNCTION__, __LINE__); 
    device_unregister(&dev->dev);
    return 0;
}
EXPORT_SYMBOL(zl_device_unregister);

int zl_driver_register(struct zl_driver *drv)
{
    drv->drv.bus = &zl_bus;

    printk("[%s %s %d ]\n", __FILE__,  __FUNCTION__, __LINE__); 
    driver_register(&drv->drv);
    return 0;
}
EXPORT_SYMBOL(zl_driver_register);

int zl_driver_unregister(struct zl_driver *drv)
{
    printk("[%s %s %d ]\n", __FILE__,  __FUNCTION__, __LINE__); 
    driver_unregister(&drv->drv);
    return 0;
}
EXPORT_SYMBOL(zl_driver_unregister);

int zl_match(struct device * dev, struct device_driver * drv)
{
    struct zl_device *zldev;
    struct zl_driver *zldrv;

    printk("[%s %s %d ]\n", __FILE__,  __FUNCTION__, __LINE__);

    zldev = container_of(dev, struct zl_device, dev);
    zldrv = container_of(drv, struct zl_driver, drv);

    if ((zldev->idVendor == zldrv->idVendor) && (zldev->idProduct == zldrv->idProduct)) {
        printk("[%s %s %d ] success\n", __FILE__,  __FUNCTION__, __LINE__); 
        return 1;
    }
    printk("[%s %s %d ] fail\n", __FILE__,  __FUNCTION__, __LINE__); 
    return 0;
}


int test_init(void)
{
    int ret = 0;

    printk("[%s %s %d ]\n", __FILE__,  __FUNCTION__, __LINE__); 
    bus_register(&zl_bus);

    return ret;
}

void test_exit(void)
{
    printk("[%s %s %d ]\n", __FILE__,  __FUNCTION__, __LINE__); 
    bus_unregister(&zl_bus);
}

module_init(test_init);
module_exit(test_exit);
